package com.king.factory;

import java.lang.reflect.Proxy;

import org.springframework.beans.factory.FactoryBean;

import com.king.proxy.KingProxy;
@SuppressWarnings("unchecked")
/**
 * 
 * <p class="detail">
 * 功能：自定义bean
 * </p>
 * @ClassName: KingBeanFactory 
 * @version V1.0  
 * @date 2014年12月17日 
 * @author <a href="mailto:wangs@financegt.com">kings</a>
 * @param <T>
 */
public class KingBeanFactory <T> implements FactoryBean<T>{
	 private Class<T>            kInterface;

	    private KingProxy proxy;

	    @Override
	    public T getObject() throws Exception {
	        return newInstance();
	    }

	    @Override
	    public Class<?> getObjectType() {
	        return kInterface;
	    }

	    public KingProxy getProxy() {
	        return proxy;
	    }


	    @Override
	    public boolean isSingleton() {
	        return true;
	    }

		private T newInstance() {
	        return (T) Proxy.newProxyInstance(kInterface.getClassLoader(),
	            new Class[] {kInterface }, proxy);
	    }

	    public void setProxy(KingProxy proxy) {
	        this.proxy = proxy;
	    }

		public Class<T> getkInterface() {
			return kInterface;
		}

		public void setkInterface(Class<T> kInterface) {
			this.kInterface = kInterface;
		}

	   
}
